﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Utiles
{
    public static class Utilidades
    {
        /// <summary>
        /// Convierte el archivo en un arreglo de bytes
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static byte[] StreamFile(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            byte[] informationData = new byte[fs.Length];
            fs.Read(informationData, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            return informationData;
        }

        /// <summary>
        /// calcula el MD5 del archivo a transmitir
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetMd5HashFromFile(string fileName)
        {
            FileStream file = new FileStream(fileName, FileMode.Open);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }

        public static string ObtenerInformacion(string indicador, string data, char separador)
        {
            foreach (string dato in data.Split(separador))
            {
                if (dato.StartsWith(indicador))
                {
                    var temp = indicador + ":";
                    return dato.Replace(temp, "");
                }
            }
            return string.Empty;
        }

    }
}
